import router from '@/router'
import { TabsState, TabsMenuProps } from '@/store/interface'
import piniaPersistConfig from '@/config/piniaPersist'
import { defineStore } from 'pinia'

export const useTabsStore = defineStore({
  id: 'tabs',
  state: (): TabsState => ({
    tabsMenuList: [],
  }),
  actions: {
    // Add Tabs
    async addTabs(tabItem: TabsMenuProps) {
      if (this.tabsMenuList.every((item) => item.path !== tabItem.path)) {
        this.tabsMenuList.push(tabItem)
      }
    },
    // Remove Tabs
    async removeTabs(tabPath: string, isCurrent = true) {
      const tabsMenuList = this.tabsMenuList
      if (isCurrent) {
        tabsMenuList.forEach((item, index) => {
          if (item.path !== tabPath) return
          const nextTab = tabsMenuList[index + 1] || tabsMenuList[index - 1]
          if (!nextTab) return
          router.push(nextTab.path)
        })
      }
      this.tabsMenuList = tabsMenuList.filter((item) => item.path !== tabPath)
    },
    // Close MultipleTab
    async closeMultipleTab(tabsMenuValue?: string) {
      this.tabsMenuList = this.tabsMenuList.filter((item) => {
        return item.path === tabsMenuValue || !item.close
      })
    },
    // Close LeftTab
    async closeLeftTab(tabsMenuValue?: string) {
      const currentIndex = this.tabsMenuList.findIndex((item) => item.path === tabsMenuValue)
      this.tabsMenuList = this.tabsMenuList.slice(currentIndex, this.tabsMenuList.length)
    },
    async closeRightTab(tabsMenuValue?: string) {
      const currentIndex = this.tabsMenuList.findIndex((item) => item.path === tabsMenuValue)
      this.tabsMenuList = this.tabsMenuList.slice(0, currentIndex + 1)
    },
    // Set Tabs
    async setTabs(tabsMenuList: TabsMenuProps[]) {
      this.tabsMenuList = tabsMenuList
    },
    // Set Tabs Title
    async setTabsTitle(title: string) {
      const nowFullPath = location.hash.substring(1)
      this.tabsMenuList.forEach((item) => {
        if (item.path == nowFullPath) item.title = title
      })
    },
  },
  persist: piniaPersistConfig('tabs'),
})
